<?php


namespace App\Http\Controllers\Api;


use App\Http\Controllers\Controller;
use App\Service\CurlRequest;
use App\Service\Notification;
use Google\Client;
use LaravelFCM\Message\OptionsBuilder;
use LaravelFCM\Message\PayloadDataBuilder;
use LaravelFCM\Message\PayloadNotificationBuilder;
use LaravelFCM\Message\Topics;
use FCM;
// FCM push发送
class PushMsgController extends Controller
{

    public function setFcmMsg()
    {
        try {

            $topicName = 'tttttttt';   // 自定义的主题名称 字符串
//             registration_id
            $registerToken = "fUTYd72WSYSceVoWBhlyTT:APA91bEXTp8stlAC_uY2Few92Qg64-fJr1zdFKhzGE0EWdLiFEQsrfPl66gC2MtJpuDE0KfFuRSaa38JFFAsVbUnvVBDG_WRdRgD3_BSJTLwh-pRrGfBM51a0emlmpKHIsIHmzIBr9LM";
            self::addTopic($topicName, $registerToken);
            # 订阅消息推送
            $notification = ['title' => 'test！！！', 'body' => 'this is a test！！'];		//推送名称 描述
            $option = ["link" => 'http://www.baidu.com/uri'];
            $webMsg = self::getWebMessage($notification, null, null, $option);	//生成推送到浏览器的消息格式

            $topicWebMsg = self::formatTopicMessage($topicName, $webMsg);			//生成主题推送消息
            return Notification::push($topicWebMsg);

        } catch (\Exception $e) {
            return [$e->getMessage(),$e->getLine(),$e->getFile()];
        }
    }



    /**
     * 将设备添加到主题
     * @author jeanku
     * @date 2019-11-22
     * @param string $topicName require 根据业务自定义的主题名称
     * @param string $registerToken require 前端授权得到的REGISTRATION_TOKEN
     * @return array
     * @throws \Exception
     */
    public static function addTopic($topicName, $registerToken)
    {
        $url = sprintf('https://iid.googleapis.com/iid/v1/%s/rel/topics/%s', $registerToken, $topicName);
        return CurlRequest::setHeader(self::getCommonHeader())->post($url);
    }



    /**
     * 非推送消息的请求header
     * @date 2019-11-22
     * @return array
     */
    protected static function getCommonHeader()
    {
        return [
            'Content-Type: application/json',
            'Authorization: key=' . env('FCM_SERVER_KEY'),		//env('FCM_SERVER_KEY')旧版服务器密钥
        ];
    }



    public static $common = [
        'name' => null,
        'data' => null,
        "notification" => null,
        "android" => null,
        "webpush" => null,
        "apns" => null,
        "fcm_options" => null,
        "token" => null,
        "topic" => null,
        "condition" => null,
    ];




    /**
     * 生成topic推送数据
     * @author jeanku
     * @date 2019-11-22
     * @param string $topic required topic key
     * @param array $messgeData required message data
     * @return array
     */
    public static function formatTopicMessage($topic, $messgeData)
    {
        self::$common['topic'] = $topic;
        self::$common['webpush'] = $messgeData;
        return ['message' => array_filter(self::$common)];
    }



    /**
     * 获取web端推送的数据
     * @author jeanku
     * @date 2019-11-22
     * @param array $notification required [
     *      'title' => '',
     *      'body' => '',
     * ]
     * @param array $data option []
     * @param array $header option []
     * @param array $fcm_options option [
     *      'link' => 'http://***'          //点击跳转的链接
     * ]
     * @return array
     */
    public static function getWebMessage($notification, $data = null, $header = null, $fcm_options = null)
    {
        return array_filter([
            'header' => $header,
            'data' => $data,
            'notification' => $notification,
            'fcm_options' => $fcm_options,
        ]);
    }

}
